//
//  WalletYesterdayProfitVC.m
//  xychain_DM_iOS
//
//  Created by Jpwang on 2018/5/28.
//  Copyright © 2018年 Jpwang. All rights reserved.
//

#import "WalletYesterdayProfitVC.h"
#import "PointInfo.h"
#import "PointItemCell.h"

@interface WalletYesterdayProfitVC () <UITableViewDelegate, UITableViewDataSource>

@property(weak, nonatomic) IBOutlet UIButton *backBt;
@property(weak, nonatomic) IBOutlet UILabel  *profitLb;
@property(weak, nonatomic) IBOutlet UILabel  *dateLb;
@property(weak, nonatomic) IBOutlet UITableView *tableView;

@property(strong, nonatomic) NSMutableArray *dataSource;

@end

@implementation WalletYesterdayProfitVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self requestData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initView {
    self.dataSource = [[NSMutableArray alloc] init];
    long long time = [JPDate currentTimeMillis];
    time = time-24*60*60*1000;
    NSInteger year = [JPDate year:time/1000.0];
    NSInteger month = [JPDate month:time/1000.0];
    NSInteger day = [JPDate day:time/1000.0];
    NSString *m = month<10?[NSString stringWithFormat:@"0%ld",month]:[NSString stringWithFormat:@"%ld",month];
    NSString *d = day<10?[NSString stringWithFormat:@"0%ld",day]:[NSString stringWithFormat:@"%ld",day];
    self.dateLb.text = [NSString stringWithFormat:@"%ld年%@月%@日", (long)year, m, d];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"PointItemCell" bundle:nil] forCellReuseIdentifier:@"PointItemCellID"];
    [[self.backBt rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        [self.navigationController popViewControllerAnimated:YES];
    }];
}

- (void)requestData {
    [[[HttpRequest shareInstance] signGet:w_yesterday_point map:nil] subscribeNext:^(id x) {
        if ([x[@"code"] intValue] == 200) {
            @try {
                id data = x[@"data"];
                float profit = data ? [data doubleValue] : 0;
                self.profitLb.text = [NSString stringWithFormat:@"%f", profit];
            } @catch (NSException *exception) {
                self.profitLb.text = @"--";
            }
        } else {
            self.profitLb.text = @"--";
        }
    } error:^(NSError *error) {
        self.profitLb.text = @"--";
    }];
    [[[HttpRequest shareInstance] signGet:w_yesterday_detail map:nil] subscribeNext:^(id x) {
        if ([x[@"code"] intValue] == 200) {
            NSError *error;
            NSArray *temp = [MTLJSONAdapter modelsOfClass:[PointInfo class] fromJSONArray:x[@"data"] error:&error];
            if (error) {
                [JPToast showText:@"数据解析错误"];
            } else {
                [self.dataSource addObjectsFromArray:temp];
                [self.tableView reloadData];
            }
        } else {
            [JPToast showText:x[@"message"]];
        }
    } error:^(NSError *error) {
        [JPToast showText:@"网络错误"];
    }];
}

#pragma mark UITableView delegate and datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    float w = self.tableView.frame.size.width;
    float h = w*140/560 + 10;
    return h;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PointItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PointItemCellID"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell performSelector:@selector(setData:) withObject:self.dataSource[indexPath.row]];
    return cell;
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}


@end
